package com.sample.library.portlet.book;

import java.util.Date;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletRequest;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.validation.BindException;
import org.springframework.web.portlet.mvc.SimpleFormController;

import com.liferay.portal.kernel.util.ParamUtil;
import com.sample.library.model.Author;
import com.sample.library.model.impl.AuthorImpl;
import com.sample.library.service.AuthorLocalServiceUtil;
import com.sample.library.service.persistence.AuthorUtil;

public class EditAuthorController extends SimpleFormController implements InitializingBean{

	@Override
	public void afterPropertiesSet() throws Exception {
	}

	@Override
	protected Object formBackingObject(PortletRequest request) throws Exception {
		long authorId = ParamUtil.getLong(request, "authorId");
		if( authorId > 0) {
			return AuthorUtil.findByPrimaryKey(authorId);
		}
		return new AuthorImpl();
	}
	
	@Override
	protected void onSubmitAction(ActionRequest request,
			ActionResponse response, Object command, BindException errors)
			throws Exception {
		Author author = (Author)command;
		
		author.setBirthDay(new Date());
		AuthorLocalServiceUtil.updateAuthor(author);
		AuthorUtil.clearCache();
	}
}
